//
//  CardView.m
//  LottieAnimationTest
//
//  Created by Felix Yin on 2019/4/12.
//  Copyright © 2019 Felix Yin. All rights reserved.
//

#import "CardView.h"

@interface CardView ()

@property(nonatomic,assign) CGFloat centerX;
@property(nonatomic,assign) CGFloat centerY;
@property(nonatomic,assign) CGPoint originalPoint;
@property(nonatomic,strong) UILabel *numLbl;

@end

@implementation CardView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    [self setupView];
    return self;
}

- (void) setupView{
    self.layer.cornerRadius = 20;
    self.layer.shadowRadius = 3;
    self.layer.shadowOpacity = 0.4;
    self.layer.shadowOffset = CGSizeMake(0.5, 3);
    self.layer.shadowColor = [UIColor darkGrayColor].CGColor;
    self.clipsToBounds = YES;
    self.userInteractionEnabled = YES;
    self.originalPoint = self.center;
    
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(beingDragged:)];
    [self addGestureRecognizer:panGesture];
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:self.bounds];
    backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
    backgroundImageView.clipsToBounds = true;
    [self addSubview:backgroundImageView];
    self.imgView = backgroundImageView;
    
    [self addSubview:self.numLbl];
    CGFloat width = 100;
    CGFloat height = 20;
    CGFloat x = (CGRectGetWidth(self.frame) - width)*0.5;
    CGFloat y = (CGRectGetHeight(self.frame) - height)*0.5;
    self.numLbl.frame = CGRectMake(x, y, width, height);
    
}

- (void) beingDragged:(UIPanGestureRecognizer *) gestureRecognizer{
    self.centerX = [gestureRecognizer translationInView:self].x;
    self.centerY = [gestureRecognizer translationInView:self].y;

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        
    }else if (gestureRecognizer.state == UIGestureRecognizerStateChanged){
        CGFloat rotationStrength = MIN(self.centerX/[UIScreen mainScreen].bounds.size.width, 1);
        CGFloat rotationAngel = M_PI/8*rotationStrength;
        CGFloat scale = MAX(1-fabs(rotationStrength)/4, 0.9);
        self.center = CGPointMake(self.originalPoint.x+self.centerX, self.originalPoint.y+self.centerY);
        CGAffineTransform transforms = CGAffineTransformMakeRotation(rotationAngel);
        CGAffineTransform scaletransforms = CGAffineTransformScale(transforms, scale, scale);
        self.transform = scaletransforms;
    }else if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
        [self afterSwiperAction];
        return;
    }else if (gestureRecognizer.state == UIGestureRecognizerStatePossible){
        
    }else if (gestureRecognizer.state == UIGestureRecognizerStateCancelled){
        
    }else if (gestureRecognizer.state == UIGestureRecognizerStateFailed){
        
    }
}


- (void) afterSwiperAction{
    CGFloat maxY = [UIScreen mainScreen].bounds.size.height*0.5 * 0.75;
    CGFloat maxX = [UIScreen mainScreen].bounds.size.width*0.5 * 0.75;
    if (self.centerX > maxX) {
        [self cardRight];
    }else if (self.centerX < -maxX){
        [self cardLeft];
    }else if (self.centerY > maxY) {
        [self cardGoesDown];
    }else if (self.centerY < -maxY){
        [self cardGoseUp];
    }else{
        [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:1 options:UIViewAnimationOptionTransitionNone animations:^{
            self.center = self.originalPoint;
            self.transform = CGAffineTransformMakeRotation(0);
        } completion:nil];
    }
}

//向左滑动
- (void) cardLeft{
    CGPoint finishPoint = CGPointMake(2*self.centerX, self.frame.size.height);
    [UIView animateWithDuration:0.5 animations:^{
        self.center = finishPoint;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    if ([self.delegate respondsToSelector:@selector(cardViewRemove:)]) {
        [self.delegate cardViewRemove:self];
    }
}

//向右滑动
- (void) cardRight{
    CGPoint finishPoint = CGPointMake(2*self.centerX+self.originalPoint.x, self.frame.size.height);
    [UIView animateWithDuration:0.5 animations:^{
        self.center = finishPoint;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    if ([self.delegate respondsToSelector:@selector(cardViewRemove:)]) {
        [self.delegate cardViewRemove:self];
    }
}

//向上滑动
- (void) cardGoseUp{
    CGPoint finishPoint = CGPointMake(2*self.centerX+self.originalPoint.x, -self.frame.size.height*2);
    [UIView animateWithDuration:0.5 animations:^{
        self.center = finishPoint;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    if ([self.delegate respondsToSelector:@selector(cardViewRemove:)]) {
        [self.delegate cardViewRemove:self];
    }
}

//向下滑动
- (void) cardGoesDown{
    CGPoint finishPoint = CGPointMake(2*self.centerX, self.frame.size.height*2);
    [UIView animateWithDuration:0.5 animations:^{
        self.center = finishPoint;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    if ([self.delegate respondsToSelector:@selector(cardViewRemove:)]) {
        [self.delegate cardViewRemove:self];
    }
}

//恢复
- (void) makeUndoAction{
    [UIView animateWithDuration:0.4 animations:^{
        self.center = self.originalPoint;
        self.transform = CGAffineTransformMakeRotation(0);
    } completion:nil];
}

- (void)setNum:(NSInteger)num{
    _num = num;
    self.numLbl.text = [@"" stringByAppendingFormat:@"%@",@(num)];
}

//    MARK: Lazy Loading

- (UILabel *)numLbl{
    if (!_numLbl) {
        _numLbl= [[UILabel alloc] init];
        _numLbl.textColor = [UIColor whiteColor];
        _numLbl.textAlignment = NSTextAlignmentCenter;
        _numLbl.font = [UIFont systemFontOfSize:20];
    }
    return _numLbl;
}

@end
